package handler

import (
	"family-doctor/api/request"
	"family-doctor/common/globals"
	pkg2 "family-doctor/common/pkg"
	"family-doctor/common/proto/familyDoctor"
	"github.com/cheng1005/XxCh-Common/pkg"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"

	"log"
	"net/http"
	"path/filepath"
	"time"
)

func UserLogin(c *gin.Context) {
	var req request.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	login, err := globals.Grpc.UserLogin(c, &familyDoctor.UserLoginRequest{
		Phone: req.Phone,
		Code:  req.Code,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	token, _ := pkg.NewJWT("2210A").CreateToken(pkg.CustomClaims{
		ID: uint(login.UserId),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: 60 * 24 * 30,
		},
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": token,
	})
}
func EventList(c *gin.Context) {
	var req request.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	list, err := globals.Grpc.EntityList(c, &familyDoctor.EntityListRequest{
		Page:    int64(req.Page),
		Size:    int64(req.Size),
		KeyWord: req.KeyWord,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": list,
	})
}

func DoctorInfo(c *gin.Context) {
	var req request.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	info, err := globals.Grpc.DoctorInfo(c, &familyDoctor.DoctorInfoRequest{
		DoctorId: req.DoctorId,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": info,
	})
}

func DoctorList(c *gin.Context) {
	var req request.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	list, err := globals.Grpc.DoctorList(c, &familyDoctor.DoctorListRequest{})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": list,
	})
}

func EventInfo(c *gin.Context) {
	var req request.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	info, err := globals.Grpc.EntityInfo(c, &familyDoctor.EntityInfoRequest{
		EntityId: req.EventId,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": info,
	})
}

func OrderCreate(c *gin.Context) {
	var req request.Request
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	userId := 1
	log.Println(userId)
	create, err := globals.Grpc.OrderCreate(c, &familyDoctor.OrderCreateRequest{
		UserId:   uint64(userId),
		DoctorId: req.DoctorId,
		PayTypes: req.PayTypes,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": create,
	})
}
func UploadImage(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")

	if file.Size > 1024*1024 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "文件太大"})
		return
	}

	ext := filepath.Ext(file.Filename)

	if ext != ".jpg" && ext != ".png" && ext != ".jpeg" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "文件类型错误"})
		return
	}

	filename := time.Now().Format("20060102150405") + ext

	dst := "./satic/image/" + filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}
	Url := pkg2.Upload(filename, dst)
	c.JSON(http.StatusOK, gin.H{"url": Url})
}
